//
//  AXHdzqPdTgVC.m
//  AnXin100
//
//  Created by 天质弘耕 on 2018/11/6.
//  Copyright © 2018 tzhg. All rights reserved.
//

#import "AXHdzqPdTgVC.h"
#import "ZXSegmentedView.h"
#import "UIView+SDAutoLayout.h"
#import "ZXCategories.h"
#import "Header.h"
#import "UIViewController+HBD.h"
#import "UITableView+SDAutoTableViewCellHeight.h"
#import "AXHdzqPdTgTopCell.h"
#import "AXHdzqPdTgMidCell.h"
#import "AXHdzqPdTgBottomCell.h"
#import "AXHdzqPdTgTopModel.h"
#import "AXHdzqPdTgMidModel.h"
#import "AXHdzqPdTgBottomModel.h"


@interface AXHdzqPdTgVC ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) NSMutableArray<AXHdzqPdTgTopModel *>  *topModelsArray;
@property (nonatomic, strong) NSMutableArray<AXHdzqPdTgMidModel *>  *midModelsArray;
@property (nonatomic, strong) NSMutableArray<AXHdzqPdTgBottomModel *> *bottomModelsArray;
@property (nonatomic ,strong) ZXSegmentedView *segmentedView;
@property (nonatomic, assign) NSInteger requstIndex;
@property (nonatomic ,strong) UITableView *tableView;



@end

@implementation AXHdzqPdTgVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupNavigationItems];
    self.view.backgroundColor = [UIColor whiteColor];
    // Do any additional setup after loading the view.
    [self creatView];
    [self setlayout];
 
  
    
}



- (void)creatView {
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    self.tableView.backgroundColor = [UIColor whiteColor];
    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    self.tableView.tableFooterView = [UIView new];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[AXHdzqPdTgTopCell class] forCellReuseIdentifier:[AXHdzqPdTgTopCell reuseID]];
    [self.tableView registerClass:[AXHdzqPdTgMidCell class] forCellReuseIdentifier:[AXHdzqPdTgMidCell reuseID]];
    [self.tableView registerClass:[AXHdzqPdTgBottomCell class] forCellReuseIdentifier:[AXHdzqPdTgBottomCell reuseID]];
    [self.view addSubview:self.tableView];
    

    
    
}




- (void)setlayout {
    
    self.tableView.sd_layout
    .leftSpaceToView(self.view, 0)
    .topSpaceToView(self.view, 0)
    .widthIs(Wi)
    .heightIs(TableViewHeight);
    
    
    
}

#pragma mark - Action



- (void)setupNavigationItems {
    
    self.hbd_barTintColor = [UIColor whiteColor];
    self.hbd_barShadowHidden = YES;
    
    //返回按钮
    UIButton *kfBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [kfBtn setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    kfBtn.frame = CGRectMake(0, 0, 30, 30);
    [kfBtn addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithCustomView:kfBtn];
    self.navigationItem.leftBarButtonItem = leftItem;
    
    /** title */
    UILabel *titleLabel = [[UILabel alloc]init];
    titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 30)];
    titleLabel.text = @"团购";
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.font = FONT_EIGHTEEN;
    titleLabel.textColor = [UIColor colorWithHex:0x29b566];
    self.navigationItem.titleView = titleLabel;
    
}

#pragma mark - Action


- (void)backAction:(UIBarButtonItem *)item {
    [self.navigationController popViewControllerAnimated:YES];
}



#pragma mark - tableViewDelegate & tableViewDatasorce

//分区头
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    
    if (section == 2) {
        
        
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 44)];
        view.backgroundColor = [UIColor whiteColor];
        
        /** 分段控制 */
        NSArray<NSString *> *titles = nil;
        titles = @[@"商品详情", @"拼团说明"];
        self.segmentedView = [[ZXSegmentedView alloc] initWithframe:CGRectMake(30 , 0, SCREEN_WIDTH/3, 44) titles:titles];
        [view addSubview:self.segmentedView];
        
        ZX_WEAK_SELF
        [self.segmentedView didSelectHandler:^(NSInteger index) {
            weakSelf.requstIndex = index;
            
        }];
        
        return view;
    }
    
    return 0;
    
    
}


/** 分区的高度 */

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    if (section == 2) {
        
        return 44;
    }
    
    return 0;
    
}


//几个分区
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (section == 2) {
        
        return 4;
    }
    
    return 1;
}

/** cell的高度 */

/** cell 自适应第二步 */
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 2) {
        
        return 60;
    }
    
    if (indexPath.section == 0) {
        
        return [tableView cellHeightForIndexPath:indexPath model:self.topModelsArray[indexPath.row] keyPath:@"model" cellClass:[AXHdzqPdTgTopCell class] contentViewWidth:SCREEN_WIDTH];
    }
    
      return [tableView cellHeightForIndexPath:indexPath model:self.midModelsArray[indexPath.row] keyPath:@"model" cellClass:[AXHdzqPdTgMidCell class] contentViewWidth:SCREEN_WIDTH];
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        
        AXHdzqPdTgTopCell *topCell = [tableView dequeueReusableCellWithIdentifier:[AXHdzqPdTgTopCell reuseID] forIndexPath:indexPath];
         [topCell setSelectionStyle:UITableViewCellSelectionStyleNone];
        return topCell;
        
        
    }
    
    else if (indexPath.section == 1) {
        
        AXHdzqPdTgMidCell *midCell = [tableView dequeueReusableCellWithIdentifier:[AXHdzqPdTgMidCell reuseID] forIndexPath:indexPath];
        [midCell  setSelectionStyle:UITableViewCellSelectionStyleNone];
        return midCell;
        
    }
    
    AXHdzqPdTgBottomCell *cell = [tableView dequeueReusableCellWithIdentifier:[AXHdzqPdTgBottomCell reuseID] forIndexPath:indexPath];
      [cell  setSelectionStyle:UITableViewCellSelectionStyleNone];
    NSArray<NSString *> *str = @[@"云南省大理市宾川镇",@"夏黑无籽葡萄 1kg装 新鲜水果 ",@"27.99",@"如您收到的商品出现损坏或缺漏，请您拍下带外 箱及外包装的水果照片并发送给我们的客服，我们将为您处理！"];
    NSArray<NSString *> *title = @[@"产地：",@"产品信息:",@"价格说明:",@"售后服务说明:"];
    cell.label.text = [NSString stringWithFormat:@"%@:%@",title[indexPath.row],str[indexPath.row]];
    if (indexPath.row == 3) {
        
        cell.label.textColor = COLOR_ORG;
    }
    
    
    return cell;
    
    
}



@end
